package pers.qianyu.month_202012.date_20201227;

import java.util.*;

/**
 * 290. 单词规律
 * https://leetcode-cn.com/problems/word-pattern/
 *
 * @author mizzle rain
 * @date 2020-12-27 15:20
 */
public class WordPattern {
    public boolean wordPattern(String pattern, String s) {
        String[] arr = s.split(" ");
        int len = arr.length;
        if (len != pattern.length()) {
            return false;
        }
        HashMap<Character, String> p2s = new HashMap<>();
        HashMap<String, Character> s2p = new HashMap<>();
        for (int i = 0; i < len; i++) {
            char ch = pattern.charAt(i);
            String st = arr[i];
            if (p2s.containsKey(ch) && !p2s.get(ch).equals(st) ||
                    s2p.containsKey(st) && s2p.get(st) != ch) {
                return false;
            }
            p2s.put(ch, st);
            s2p.put(st, ch);
        }
        return true;
    }
}
